#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/boot.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>

//------------------------------------------
unsigned int tmp4;
unsigned int res4;

#define ISC( a, b ) ISC ## a ## b
#define TMP( a ) tmp ## a
#define RES( a ) res ## a
#define GetPulse( a ) GetPulse ## a



#define PULSE_METER( index )                                               \
unsigned int TMP(index);                                                   \
unsigned int RES( index );                                                 \
ISR(INT ## index ## _vect)                                                 \
{                                                                          \
	if ( EICRB & ( 1 << ISC( index, 1 ) ) )                                \
	{                                                                      \
		/* raising */                                                      \
		TMP( index ) = TCNT3;                                              \
		EICRB &= ~( 1 << ISC( index, 1 ) );                                \
	}                                                                      \
	else                                                                   \
	{                                                                      \
		/* lower */                                                        \
		RES( index ) = TCNT3;                                              \
		if ( tmp4 <= RES( index ) )                                        \
		{                                                                  \
			RES( index ) = RES( index ) - TMP( index );                    \
		}                                                                  \
		else                                                               \
		{                                                                  \
			RES( index ) = 0xffff - ( TMP( index ) - RES( index ) );       \
		}                                                                  \
		EICRB |=  ( 1 << ISC( index, 1 ) );                                \
	}                                                                      \
}                                                                          \
int GetPulse( index ) ()                                                   \
{                                                                          \
	return RES( index );                                                   \
}                                                                          


PULSE_METER( 4 )
PULSE_METER( 5 )
PULSE_METER( 6 )
PULSE_METER( 7 )


void MeasurePulses_Init()
{
	//timer 3 no prescaler
	TCCR3B = (1 << CS30); 

	//timer 3 / 32
	TCCR3B = (1 << CS31) | (1 << CS30);  
	 
	TCCR3A &= ~( (0<<WGM30) | (0<<WGM31) );
	TCCR3B &= ~( (0<<WGM32) | (0<<WGM33) );


	//enable int 4
	EIMSK |= (1<<4);

	//int on raising
	EICRB |=  (1<<ISC40);
	EICRB |=  (1<<ISC41);

	//enable int 5
	EIMSK |= (1<<5);

	//int on raising
	EICRB |=  (1<<ISC50);
	EICRB |=  (1<<ISC51);

	//enable int 6
	EIMSK |= (1<<6);

	//int on raising
	EICRB |=  (1<<ISC60);
	EICRB |=  (1<<ISC61);

	//enable int 7
	EIMSK |= (1<<7);

	//int on raising
	EICRB |=  (1<<ISC70);
	EICRB |=  (1<<ISC71);


}


